const TOKEN_KEY = "app-token";
const USER_INFO_KEY = "user-info";
const DICT_KEY = "dict";
const REMEMBER_PASSWORD_KEY = "remember-password";
const SAVED_USERNAME_KEY = "saved-username";
const SAVED_PASSWORD_KEY = "saved-password";
import { type DictItemOption } from "@/api/system/dict";

// 设置 token
export function setToken(token: string) {
  uni.setStorageSync(TOKEN_KEY, token);
}

// 获取 token
export function getToken(): string {
  return uni.getStorageSync(TOKEN_KEY) || "";
}

// 清除 token
export function clearToken() {
  uni.removeStorageSync(TOKEN_KEY);
}

// 设置用户信息
export function setUserInfo(userInfo: any) {
  uni.setStorageSync(USER_INFO_KEY, userInfo);
}

// 获取用户信息
export function getUserInfo(): any {
  return uni.getStorageSync(USER_INFO_KEY) || null;
}

// 清除用户信息
export function clearUserInfo() {
  uni.removeStorageSync(USER_INFO_KEY);
}

// 设置字典缓存
export function setDictCache(dict: Record<string, DictItemOption[]>) {
  uni.setStorageSync(DICT_KEY, dict);
}

// 获取字典缓存
export function getDictCache(): Record<string, DictItemOption[]> {
  return uni.getStorageSync(DICT_KEY) || {};
}

// 清除字典缓存
export function clearDictCache() {
  uni.removeStorageSync(DICT_KEY);
}

// 清除所有缓存信息
export function clearAll() {
  clearToken();
  clearUserInfo();
  clearDictCache();
}

// 设置记住密码状态
export function setRememberPassword(remember: boolean) {
  uni.setStorageSync(REMEMBER_PASSWORD_KEY, remember);
}

// 获取记住密码状态
export function getRememberPassword(): boolean {
  return uni.getStorageSync(REMEMBER_PASSWORD_KEY) || false;
}

// 保存用户名和密码
export function saveLoginInfo(username: string, password: string) {
  uni.setStorageSync(SAVED_USERNAME_KEY, username);
  uni.setStorageSync(SAVED_PASSWORD_KEY, password);
}

// 获取保存的用户名
export function getSavedUsername(): string {
  return uni.getStorageSync(SAVED_USERNAME_KEY) || "";
}

// 获取保存的密码
export function getSavedPassword(): string {
  return uni.getStorageSync(SAVED_PASSWORD_KEY) || "";
}

// 清除保存的登录信息
export function clearSavedLoginInfo() {
  uni.removeStorageSync(SAVED_USERNAME_KEY);
  uni.removeStorageSync(SAVED_PASSWORD_KEY);
}
